package cn.kc.jdbc.dao;

import cn.kc.jdbc.entity.Student;
import cn.kc.jdbc.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.lang.model.element.VariableElement;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author mosin
 * date 2022/2/14
 * @version 1.0
    学生数据持久化
 */
public class StudentDaoImpl implements StudentDao {


    @Override
    public int insert(Student student) {

        //获取数据库连接池
        DataSource dataSource = DruidUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);

        String sql  = "insert into stu (id,stu_name) values(?,?)";

        //执行sql
        try {
            int line = runner.update(sql, student.getId(), student.getStuName());
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

    @Override
    public int delById(Integer id) {
        DataSource dataSource = DruidUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);
        String sql = "delete from stu where id = ?";
        try {
            int line = runner.update(sql, id);
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateStudent(Integer stuId, long classId) {
        DataSource dataSource = DruidUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);
        String sql = "update stu set class_id = ? where  id = ?";
        int line = 0;
        try {
            line = runner.update(sql, classId, stuId);
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return line;
    }
    @Override
    public List<Student> findAll() {
        DataSource dataSource = DruidUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);
        String sql = "select stu.id , stu.stu_name  as stuName, stu.class_id  as classId ,class_name as className from stu left join class on stu.class_id = class.id ";
        try {
            List<Student> students = runner.query(sql, new BeanListHandler<>(Student.class));
            return  students;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Student selectOne(String stuName) {
        DataSource dataSource = DruidUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);
        String sql = "select id from stu where stu_name = ?";
        try {
            Student student = runner.query(sql, new BeanHandler<>(Student.class), stuName);
            return  student;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
